#include "ParseEngine.h"

//const char* PPL_INFILE_PATH = "./pcap/";
const char* PPL_INFILE_PATH = "/data/GDdata_20130618_20130619/";

ParseEngine::ParseEngine()
{}

int ParseEngine::run()
{
	PktReader pktReader; 
	PktParser pktParser; 
	Packet pkt; 

	struct dirent **nameList; 
	int fileCount = 0; 
	int fileNum = 0; 
	if (0 > (fileCount = scandir(PPL_INFILE_PATH, &nameList, 0, alphasort))) 
	{ 
		printf("Error: Input File Path Not Supported!\n"); 
		return -1; 
	} 
	else 
	{ 
		int i = 0; 
		char fileFullName[1024];
		while(fileNum < fileCount) 
		{ 
			//All Regular File 
			if(nameList[fileNum]->d_type & DT_REG) 
			{ 
				sprintf(fileFullName, "%s%s", PPL_INFILE_PATH, nameList[fileNum]->d_name);
				printf("%4d: Processing File \"%s\".\n", ++i, fileFullName); 

				FILE* fp; 
				if(!(fp = fopen(fileFullName, "r"))) 
				{ 
					printf("Error: Cannot Open Input File!\n"); 
					return -1; 
				} 

				if(0 <= pktReader.setCurFp(fp))
				{
					while(0 <= pktReader.readPkt(&pkt)) 
					{	 
						if(0 > pktParser.parsePkt(&pkt))
						{
							//Parse Cannot Continue 
						}
					}
				} 
				fclose(fp); 
			} 

			++fileNum; 
			free(nameList[fileNum]); 
		} 
		free(nameList); 
	}

	return 0;
}
